package com.qdsg.ysg.doctor.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.rest.business.RestProxy;
import com.rest.observer.FileDownLoadObserver;

import java.io.File;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

/**
 * Created by Administrator on 2018/1/2.
 */

public class ImageLoaderHelper {
    private static class Instance {
        static ImageLoaderHelper instance = new ImageLoaderHelper();
    }

    public static ImageLoaderHelper getInstance() {
        return Instance.instance;
    }

    private ImageLoaderHelper() {
    }

    public void GlideImageLoader(Context context, String url, ImageView imageView, int defaultPic) {
        Glide.with( context.getApplicationContext() ).load( url ).asBitmap().format( DecodeFormat.PREFER_ARGB_8888 ).placeholder( defaultPic ).into( imageView );
    }

    public void GlideImageLoader(Context context, String url, ImageView imageView) {
        Glide.with( context.getApplicationContext() ).load( url ).asBitmap().format( DecodeFormat.PREFER_ARGB_8888 ).into( imageView );
    }

    public void GlideImageLoader(Context context, String url, SimpleTarget<Bitmap> s) {
        Glide.with( context.getApplicationContext() ).load( url ).asBitmap().format( DecodeFormat.PREFER_ARGB_8888 ).into( s );
    }

    /**
     * Glide加载圆形图片
     */
    public void GlideCircleImageLoader(final Context context, String url, final ImageView imageView, final int defaultPic) {
//        if (TextUtils.isEmpty(url))
//            url = "www.joyoung.com";

        Glide.with( context.getApplicationContext() ).load( url )
                .asBitmap().placeholder( defaultPic ).centerCrop()
                .diskCacheStrategy( DiskCacheStrategy.SOURCE )
                .into( new BitmapImageViewTarget( imageView ) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable circularBitmapDrawable =
                                RoundedBitmapDrawableFactory.create( context.getApplicationContext().getResources(), resource );
                        circularBitmapDrawable.setCircular( true );
                        imageView.setImageDrawable( circularBitmapDrawable );
                    }
                } );
    }

    public void GlideCircleImageLoader(final Context context, String url, final ImageView imageView) {
        Glide.with( context.getApplicationContext() ).load( url ).asBitmap()
                .diskCacheStrategy( DiskCacheStrategy.SOURCE )
                .dontAnimate()
                .centerCrop().into( new BitmapImageViewTarget( imageView ) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create( context.getApplicationContext().getResources(), resource );
                circularBitmapDrawable.setCircular( true );
                imageView.setImageDrawable( circularBitmapDrawable );
            }
        } );
    }

    public void GlideImageLoaderByStream(final Context context, final String url, final ImageView imageView) {
        File pictureDirOri = new File( context.getExternalFilesDir( Environment.DIRECTORY_PICTURES ).getAbsolutePath() + "/OriPicture" );
        String imgNameOri = "HomePic_" + System.currentTimeMillis();

        RestProxy.getInstance().downloadFile( url, pictureDirOri.getAbsolutePath(), imgNameOri + ".jpg", new FileDownLoadObserver<File>() {
            @Override
            public void onDownLoadSuccess(File file) {
                Glide.with( context.getApplicationContext() ).load( file ).into( imageView );
            }

            @Override
            public void onDownLoadFail(Throwable throwable) {

            }

            @Override
            public void onProgress(int progress, long total) {

            }
        } );
    }


}
